package com.donggua.aichebao.commonui.message;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.donggua.aichebao.commonui.R;

/**
 * Created by Administrator on 2017-07-22.
 */
// com.donggua.common.ui.message.MessagItemLayout
public class MessagItemLayout extends LinearLayout {
    private ImageView imgTitle;
    private TextView tvStyle;
    private TextView tvNumber;
    private Drawable drawable;
    private View viewMessageItemLayout;
    private String title;
    private String number;
    private View viewLine;
    private static final int defauleColor = 0xff00ff00;
    public MessagItemLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.setOrientation(LinearLayout.VERTICAL);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CustomMessage);
        drawable = ta.getDrawable(R.styleable.CustomMessage_msgDrawable);
        title = ta.getString(R.styleable.CustomMessage_msgTitle);
        number = ta.getString(R.styleable.CustomMessage_msgNumber);
        LayoutInflater inflater = LayoutInflater.from(context);
        this.setGravity(Gravity.CENTER_HORIZONTAL);
        viewMessageItemLayout = inflater.inflate(R.layout.message_item_layout, this,
                true);
        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
                   ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        viewLine = new View(context);
        int lineColor 	= ta.getColor(R.styleable.CustomMessage_lineColor,defauleColor );
        viewLine.setBackgroundColor(lineColor);
        layoutParams.height = 5;
        int margin = (int)context.getResources().getDimension(R.dimen.margin);
        layoutParams.topMargin = margin *2;
        viewLine.setLayoutParams(layoutParams);
        this.addView(viewLine);
        ta.recycle();
    }
    @Override
    protected void onFinishInflate() {
        imgTitle = (ImageView)viewMessageItemLayout.findViewById(R.id.imgMessagImg);
        tvStyle = (TextView)viewMessageItemLayout.findViewById(R.id.tvStyle);
        tvNumber = (TextView)viewMessageItemLayout.findViewById(R.id.tvMessageNum);
        if(drawable != null){
            imgTitle.setImageDrawable(drawable);
        }
        tvStyle.setText(title);
        if(number == null || number.length() == 0){
            tvNumber.setVisibility(View.GONE);
        }else{
            tvNumber.setText(number);
        }
        super.onFinishInflate();
    }

    public void setNumber(int number){
        tvNumber.setVisibility(View.VISIBLE);
        tvNumber.setText(String.valueOf(number));
    }
}
